#region The MIT License
// The MIT License
// 
// Copyright (c) 2009 octalforty studios
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#endregion
using System;
using System.IO;
using System.Text;

using NUnit.Framework;

using octalforty.Componento.ComponentModel.Installation;
using octalforty.Componento.ComponentModel.Installation.FileSystem;
using octalforty.Componento.Shared.SemanticModel;
using Rhino.Mocks;

namespace octalforty.Componento.Tests.Componento.ComponentModel.Installation.FileSystem
{
    [TestFixture()]
    public class DirectoryManifestProviderTestFixture
    {
        [Test()]
        public void CanProvideFromDirectory()
        {
            MockRepository mockRepository = new MockRepository();
            IFileSystemManagerService fileSystemManager = mockRepository.StrictMock<IFileSystemManagerService>();

            using(mockRepository.Record())
                Expect.Call(fileSystemManager.DirectoryExists(@"e:\lib\nhibernate")).
                    Return(true);

            DirectoryManifestProvider manifestProvider = new DirectoryManifestProvider();
            manifestProvider.FileSystemManager = fileSystemManager;

            using(mockRepository.Playback())
                Assert.IsTrue(manifestProvider.CanProvideManifestFrom(new Uri(@"file:///e:\lib\nhibernate")));
        }

        [Test()]
        public void CannnotProvideFromNonexistingDirectory()
        {
            MockRepository mockRepository = new MockRepository();
            IFileSystemManagerService fileSystemManager = mockRepository.StrictMock<IFileSystemManagerService>();

            using(mockRepository.Record())
                Expect.Call(fileSystemManager.DirectoryExists(@"e:\lib\nhibernate")).
                    Return(false);

            DirectoryManifestProvider manifestProvider = new DirectoryManifestProvider();
            manifestProvider.FileSystemManager = fileSystemManager;

            using(mockRepository.Playback())
                Assert.IsFalse(manifestProvider.CanProvideManifestFrom(new Uri(@"file:///e:\lib\nhibernate")));
        }

        [Test()]
        public void CannotProvideFromHttp()
        {
            Assert.IsFalse(
                new DirectoryManifestProvider().CanProvideManifestFrom(new Uri("http://www.foo/bar")));
        }

        [Test()]
        public void GetManifestFromDirectoryWithVersionNumber()
        {
            MockRepository mockRepository = new MockRepository();
            IFileSystemManagerService fileSystemManager = mockRepository.StrictMock<IFileSystemManagerService>();

            using(mockRepository.Record())
                Expect.Call(fileSystemManager.FileExists(@"e:\lib\NHibernate-2.1.0\dependencies.txt")).
                    Return(false);

            DirectoryManifestProvider manifestProvider = new DirectoryManifestProvider();
            manifestProvider.FileSystemManager = fileSystemManager;

            ComponentManifest manifest = 
                manifestProvider.GetComponentManifest("nhibernate", new Uri(@"file:///e:\lib\NHibernate-2.1.0"), null);

            Assert.AreEqual(1, manifest.Resources.Count);
            Assert.AreEqual(new Version(2, 1, 0), manifest.Descriptor.Version);
            Assert.IsFalse(manifest.IsMetamanifest);

            Assert.AreEqual(@"e:\lib\NHibernate-2.1.0", manifest.Resources[0].Uri.LocalPath);
        }

        [Test()]
        public void GetManifestFromDirectoryWithVersionNumberAndVersionRequirement()
        {
            MockRepository mockRepository = new MockRepository();
            IFileSystemManagerService fileSystemManager = mockRepository.StrictMock<IFileSystemManagerService>();

            using (mockRepository.Record())
                Expect.Call(fileSystemManager.FileExists(@"e:\lib\nhibernate-2.1.0\dependencies.txt")).
                    Return(false);

            DirectoryManifestProvider manifestProvider = new DirectoryManifestProvider();
            manifestProvider.FileSystemManager = fileSystemManager;

            ComponentManifest manifest =
                manifestProvider.GetComponentManifest("nhibernate", 
                    new Uri(@"file:///e:\lib\nhibernate-2.1.0"), 
                    VersionSpecification.Equal(new Version(5, 0)));

            Assert.IsNull(manifest);
        }

        [Test()]
        public void GetManifestFromDirectoryWithVersionRequirement()
        {
            MockRepository mockRepository = new MockRepository();
            IFileSystemManagerService fileSystemManager = mockRepository.StrictMock<IFileSystemManagerService>();

            using (mockRepository.Record())
                Expect.Call(fileSystemManager.FileExists(@"e:\lib\nhibernate\dependencies.txt")).
                    Return(false);

            DirectoryManifestProvider manifestProvider = new DirectoryManifestProvider();
            manifestProvider.FileSystemManager = fileSystemManager;

            ComponentManifest manifest =
                manifestProvider.GetComponentManifest("nhibernate",
                    new Uri(@"file:///e:\lib\nhibernate"), 
                    VersionSpecification.Equal(new Version(5, 0)));

            Assert.AreEqual(1, manifest.Resources.Count);
            Assert.AreEqual(new Version(5, 0), manifest.Descriptor.Version);
            Assert.IsFalse(manifest.IsMetamanifest);

            Assert.AreEqual(@"e:\lib\nhibernate", manifest.Resources[0].Uri.LocalPath);
        }
    }
}
